package com.galaxy.configuration;

import com.galaxy.service.MinioService;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * @author zgx
 * @date 2025/1/10 9:50
 **/
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
@ConditionalOnClass(value = MinioClient.class)
public class MinioAutoConfiguration {

    private final MinioProperties minioProperties;

    public MinioAutoConfiguration(MinioProperties minioProperties) {
        this.minioProperties = minioProperties;
    }

    @Bean
    public MinioClient minioClient() {
        MinioClient.Builder builder = MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey());

        Optional.ofNullable(minioProperties.getRegion()).filter(StringUtils::hasText).ifPresent(builder::region);
        return builder.build();
    }

    @Bean
    public MinioService minioService() {
        return new MinioService(minioClient());
    }
}
